package com.brainshare.web.controller.an;

import com.brainshare.an.domain.AnWechatUser;
import com.brainshare.an.service.IAnWechatUserService;
import com.brainshare.common.annotation.RepeatSubmit;
import com.brainshare.common.constant.ServiceExceptionStatus;
import com.brainshare.common.core.controller.BaseController;
import com.brainshare.common.core.domain.AjaxResult;
import com.brainshare.common.utils.StringUtils;
import com.brainshare.system.domain.param.UserInfo1Param;
import com.brainshare.system.domain.param.UserInfo2Param;
import com.brainshare.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@RestController
@RequestMapping("/wechatuser")
public class WechatUserController extends BaseController
{

    @Autowired
    IAnWechatUserService anWechatUserService;

    /**
     * 发送短信验证码
     *
     * @param map
     * @return
     */
    @RepeatSubmit
    @PostMapping("/sendSmsCode")
    public AjaxResult sendSmsCode(@RequestBody Map<String, Object> map)
    {
        String phone = map.get("phone").toString();
        if (StringUtils.isEmpty(phone))
        {
            return AjaxResult.error("手机号不能为空");
        }
        boolean result = anWechatUserService.SendSmsCode(phone);
        if (result)
        {
            return AjaxResult.success("发送短信验证码成功！");
        }
        return AjaxResult.error(ServiceExceptionStatus.CODE_ERROR, "短信发送重复或短信发送失败！");
    }

    /**
     * 会员登录
     *
     * @param map
     * @return
     */
    @RepeatSubmit
    @PostMapping("/login")
    public AjaxResult anWechatUserLogin(@RequestBody Map<String, Object> map)
    {
        String phone = map.get("phone").toString();
        String smsCode = map.get("smsCode").toString();
        String mechanismId = "";
        if (map.get("mechanismId") != null)
        {
            mechanismId = map.get("mechanismId").toString();
        }
        String shareId = "";
        if (map.get("shareId") != null)
        {
            shareId = map.get("shareId").toString();
        }
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(smsCode))
        {
            return AjaxResult.error("手机号或验证码不能为空！");
        }
        String token = anWechatUserService.anWechatUserLogin(phone, smsCode, mechanismId, shareId);
        return AjaxResult.success("会员登录成功！", token);
    }

    @RepeatSubmit
    @PostMapping("/add_user_info1")
    public AjaxResult addUserInfo1(HttpServletRequest request,@RequestBody UserInfo1Param userInfo1Param)
    {
        return AjaxResult.success(anWechatUserService.addUserInfo1(request,userInfo1Param));
    }

    @RepeatSubmit
    @PostMapping("/add_user_info2")
    public AjaxResult addUserInfo2(HttpServletRequest request,@RequestBody UserInfo2Param userInfo2Param)
    {
        return AjaxResult.success(anWechatUserService.addUserInfo2(request,userInfo2Param));
    }

    //@RepeatSubmit
    //@GetMapping("/init_user")
    public AjaxResult initUser(HttpServletRequest request,String phone)
    {
        return AjaxResult.success(anWechatUserService.initUser(request,phone));
    }

    /**
     * 用户退出登录
     * @param request
     * @return
     */
    @PostMapping("/loginOut")
    public AjaxResult anWechatUserLoginOut(HttpServletRequest request)
    {
        boolean result = anWechatUserService.anWechatUserLoginOut(request);
        if (result)
        {
            return AjaxResult.success("会员退出登录成功！");
        }
        return AjaxResult.error("会员退出登录失败！");
    }

    /**
     * 获取分享连接
     */
    @PostMapping("/getShareLink")
    public AjaxResult getAnWechatUserShareLink(HttpServletRequest request)
    {
        AnWechatUser anWechatUser = CommonUtils.getAnWechatUser(request);
        String shareLink = anWechatUserService.getAnWechatUserShareLink(anWechatUser);
        if (!StringUtils.isNullOrEmpty(shareLink))
        {
            return AjaxResult.success("获取分享连接成功", shareLink);
        }
        return AjaxResult.error("获取分享连接失败！");
    }
}
